package com.lepu.app.fun.tool.ymsjb.util;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.app.config.UserConfig;
import com.app.utils.Const;
import com.app.utils.LoginUtil;
import com.app.utils.Setting;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.main.LogUtilBase;

public class YiMiaoUtil implements AsyncRequest {

	private static final String REQUEST_JQTX_DATA = "request_jqtx_data";

	private static YiMiaoUtil mInstance = null;
	private static Context mContext = null;

	private YiMiaoUtil() {

	}

	public static YiMiaoUtil getInstance(Context c) {
		if (mInstance == null) {
			mContext = c;
			mInstance = new YiMiaoUtil();
		}
		return mInstance;
	}

	// ==============================================
	public void requestData() {

		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();
		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);
			params.put("UserID", LoginUtil.getUserId());
			params.put("LoginToken", LoginUtil.getLoginToken());
			params.put("ToolID", "2");
		} catch (Exception e) {
		}
		map.put("ht", params);

		ApiClient.http_post_main(Setting.getdownloadYiMiaoUrl(), map, null,this, REQUEST_JQTX_DATA, false);
	}
	
	
	public void cleanData(){
		//清除标志
		UserConfig.setConfig(mContext, Const.CONFIG_YIMIAO_BABY_DATE, "");
		UserConfig.setConfig(mContext, Const.CONFIG_YIMIAO_REMIND_IS, false);
		UserConfig.setConfig(mContext, Const.CONFIG_YIMIAO_REMIND_AN, 0);
		UserConfig.setConfig(mContext, Const.CONFIG_YIMIAO_REMIND_TIME, "");
	}


	// ==============================================
	@Override
	public void RequestComplete(Object object, Object data) {
		if (object.equals(REQUEST_JQTX_DATA)) {

			 try {
		            JSONObject jsonObject = new JSONObject((String)data);
		            JSONObject obj1 = (JSONObject) jsonObject.get("Result");
		            JSONObject obj2 = obj1.getJSONObject("DetailInfo");
		            
		            String babyDate = obj2.optString("BabyBirthday");
		            int isRemind = obj2.optInt("IsAlert");
		            int remindAn = obj2.optInt("AlertBeforeDay");
		            String remindTime = obj2.optString("AlertTime");

		            boolean remindIs = false;
		            if(isRemind == 0){
		            	remindIs = false;
		            }else if(isRemind == 1){
		            	remindIs = true;
		            }
		            
					//保存标志
		    		UserConfig.setConfig(mContext, Const.CONFIG_YIMIAO_BABY_DATE, babyDate);
					UserConfig.setConfig(mContext, Const.CONFIG_YIMIAO_REMIND_IS, remindIs);
					UserConfig.setConfig(mContext, Const.CONFIG_YIMIAO_REMIND_AN, remindAn);
					UserConfig.setConfig(mContext, Const.CONFIG_YIMIAO_REMIND_TIME, remindTime);
		        } catch (JSONException e) {
		            e.printStackTrace();
		        }
			
		}
	}

	@Override
	public void RequestError(Object object, int errorId, String data) {
		if (object.equals(REQUEST_JQTX_DATA)) {
			LogUtilBase.LogD(null, "REQUEST_JQTX_DATA fail");
		}
	}
}
